package GUI;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import javax.imageio.ImageIO;

import util.Util;

public class ImageStorage {
	private static ImageStorage instance = null;
	
	private Vector<BufferedImage> imgList = new Vector<BufferedImage>();
	private HashMap<String, BufferedImage> imgMap = new HashMap<String, BufferedImage>();
	private Vector<String> blackList = new Vector<String>();
	
	private ImageStorage()
	{
	}
	
	public static ImageStorage get()
	{
		if(instance == null) instance = new ImageStorage();
		return instance;
	}
	
	private BufferedImage loadImage(String name)
	{
		try {
			BufferedImage img = ImageIO.read(new File(Util.fixPath(name)));
			imgList.add(img);
			imgMap.put(name, img);
			return img;
		} catch (IOException e) {
			blackList.add(name);
			System.out.println("Image not found, blacklisted : " + name);
			return null;
		}
	}
	
	private BufferedImage getImg(String name)
	{
		BufferedImage img = imgMap.get(name);
		if(img != null) return img;
		
		if(blackList.contains(name)) return null;
		return loadImage(name);
	}
	
	public static BufferedImage getImage(String name)
	{
		return get().getImg(name);
	}
}
